$(function () {
    $("#link_reg").on("click", function () {
        $(".login-box").hide();
        $(".reg-box").show();
    })
    $("#link_login").on("click", function () {
        $(".login-box").show();
        $(".reg-box").hide();
    })
    layui.form.verify({
        pass: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repwd: (res) => {
            let pwdValue = $("#paw").val().trim();
            if (pwdValue !== res) return '两次密码不一致'
        }

    })
    // console.log($("#reg_submit"));
    $("#reg_submit").on('submit', function (e) {
        e.preventDefault();
        let data = $(this).serialize();
        $.ajax({
            method: "post",
            url: "/api/reguser",
            data,
            success: function (res) {
                const {
                    status,
                    message
                } = res;
                console.log(res);
                if (status !== 0) return layui.layer.msg(message);
                layui.layer.msg(message);
                $("#link_login").click();
            }
        })
    })

    // console.log($("#login_submit"));
    $("#login_submit").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            method: "post",
            url: "/api/login",
            data: $(this).serialize(),
            success: function (res) {
                const {
                    status,
                    message,
                    token
                } = res;
                if (status !== 0) return layui.layer.msg(message);
                console.log(res);
                layui.layer.msg(message);
                sessionStorage.setItem('token',token);
                location.href='/index.html';
            }
        })
    })
})